﻿using PluginAPI.Core;
using SCPSLAudioApi.AudioCore;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using VoiceChat;


namespace Plugin.Mono
{
    public class TriggerArea : MonoBehaviour
    {
        List<ReferenceHub> hubs = new List<ReferenceHub>();

        public void OnTriggerEnter(Collider collider)
        {
            GameObject player = collider.gameObject;
            Player triggerPlayer = Player.Get(player);

            if (triggerPlayer == null) return;
            if (!triggerPlayer.IsHuman) return;


            //if (!CodeLoad.Singleton.IsReadyContain)
            //{
            //    string message = $"<color=#E4080A>【收容程序】</color>准备中，需要等待{CodeLoad.Singleton.Config.SCP106Event.TeamRespawnCountEnable}波次增援后开启|当前波次：{CodeLoad.Singleton.Current}|评估收容材料：人类x3";
            //    MessageStringUtils.GenerateAdminMessage(message, out string output);
            //    triggerPlayer.SendBroadcast(output, 5, Broadcast.BroadcastFlags.Normal, true);

            //    return;
            //}

            ReferenceHub playerHub = triggerPlayer.ReferenceHub;
            if (!hubs.Contains(playerHub)) hubs.Add(playerHub);

            this.DoCheckCondition();
        }
        public void OnTriggerExit(Collider collider)
        {
            GameObject player = collider.gameObject;

            Player triggerPlayer = Player.Get(player);
            if (triggerPlayer != null && triggerPlayer.IsHuman)
            {
                ReferenceHub playerHub = triggerPlayer.ReferenceHub;
                hubs.Remove(playerHub);
            }
        }



        public void Clear()
        {
            if (hubs == null) hubs = new List<ReferenceHub>();
            hubs.Clear();
        }

        private void DoCheckCondition()
        {
            if (hubs.Count == 0) return;

            if (hubs.Count < 3)
            {
                Log.Info($"SCP106Event|{hubs.Count}");

                string mission = $"<color=#E4080A>SCP-106</color> 收容程序 | [当前] {hubs.Count}/ [要求] 3";
                MessageStringUtils.GenerateAdminMessage(mission, out string missionInfo);

                hubs.ForEach(e =>
                {
                    Player player = Player.Get(e);
                    player.SendBroadcast(missionInfo, 15, Broadcast.BroadcastFlags.Normal, true);
                });

                return;
            }

            if (hubs.Count == 3)
            {
                string message = "你参与了收容 SCP-106 程序。你的牺牲将被铭记！";
                MessageStringUtils.GenerateAdminMessage(message, out string messageInfo);

                hubs.ForEach(e =>
                {
                    Player player = Player.Get(e);
                    player.SetRole(PlayerRoles.RoleTypeId.Spectator, PlayerRoles.RoleChangeReason.Died);
                    player.SendBroadcast(messageInfo, 15, Broadcast.BroadcastFlags.Normal, true);
                });
                hubs.Clear();

                StartCoroutine(IDoPlayContain());

                return;
            }
        }



        private IEnumerator IDoPlayContain()
        {
            //Log.Info($"SCP106Event|进入音频播放：");

            //AudioPlayerBase baseAudio = AudioPlayerBase.Get(CodeLoad.Singleton.AudioHub);

            //Log.Info($"SCP106Event|获取 AudioPlayerBase");
            //baseAudio.BroadcastChannel = VoiceChatChannel.Intercom;
            //baseAudio.BroadcastTo = Player.GetPlayers().Select(p => p.PlayerId).ToList();
            //baseAudio.Volume = 70;
            //baseAudio.ClearOnFinish = false;
            //baseAudio.Enqueue(CodeLoad.Singleton.Config.SCP106Event.Path, -1);
            //baseAudio.Play(-1);
            //Log.Info($"SCP106Event|音频播放");

            //transform.GetComponent<Collider>().enabled = false;
            //yield return new WaitForSeconds(34f);

            //baseAudio.ShouldPlay = false;
            //baseAudio.BroadcastChannel = VoiceChatChannel.Proximity;

            //List<Player> scp106s = Player.GetPlayers().FindAll(e => e.Role == PlayerRoles.RoleTypeId.Scp106);
            //if (scp106s != null && scp106s.Count != 0)
            //{
            //    scp106s.ForEach(e => e.SetRole(PlayerRoles.RoleTypeId.Spectator, PlayerRoles.RoleChangeReason.Died));
            //}

            //Cassie.Message("SCP-106 Contain Successfully");
            //Cassie.Message("SCP-106 已被 收容程序 强制收容...", false, false, true);
            yield break;
        }

        public void TryRemove(ReferenceHub referenceHub)
        {
            if (hubs.Contains(referenceHub))
            {
                hubs.Remove(referenceHub);
            }
        }
    }
}
